import 'package:flutter/material.dart';



class TabBarIndex extends StatefulWidget {
  const TabBarIndex({Key? key,  this.title="TabBar"}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  State<TabBarIndex> createState() => _TabBarIndexState();
}

class _TabBarIndexState extends State<TabBarIndex>  with SingleTickerProviderStateMixin{
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    });
  }

  int _currentActiveTabIndex = 1;
  int _latestActiveTabIndex = 1;

  List<Color> _indicatorColorList = [
    Colors.red,
    Colors.orange,
    Colors.purpleAccent
  ];

  Color get _indicatorColor => _indicatorColorList[_currentActiveTabIndex];
  late final _animationControl = AnimationController(vsync: this,duration: Duration(seconds: 3));
  late Animation<Color?> _colorAnimation ;
  Color get _colorTweenBegin => _indicatorColorList[_latestActiveTabIndex];
  Color get _colorTweenEnd => _indicatorColorList[_currentActiveTabIndex];
  @override
  void initState() {
    super.initState();
    _colorAnimationForward();
  }


  _colorAnimationForward(){
    _colorAnimation = ColorTween(begin:_colorTweenBegin,end:_colorTweenEnd).animate(_animationControl)
      ..addListener(() {
        setState(() {
          print('animation:${_colorAnimation.value}');
        });
      });
    _animationControl.forward();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _animationControl.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return DefaultTabController(
      length: 3,
      initialIndex: _currentActiveTabIndex,
      child: Scaffold(
        appBar: AppBar(
          // Here we take the value from the MyHomePage object that was created by
          // the App.build method, and use it to set our appbar title.
          title: Text(widget.title),
          bottom:
          TabBar(
              indicatorColor: _colorAnimation.value,
              onTap: (int index) {
                setState(() {
                  _latestActiveTabIndex = _currentActiveTabIndex;
                  _currentActiveTabIndex = index;
                  _colorAnimationForward();
                });
              },
              tabs: [
                Tab(text: '电影', icon: Icon(Icons.translate)),
                Tab(text: '音乐', icon: Icon(Icons.museum)),
                Tab(
                    text: '篮球',
                    icon: Icon(Icons.baby_changing_station_outlined)),
              ]),
        ),
        body: TabBarView(
          children: [
            // Center(key:ValueKey('1'),child: Text('电影',style:TextStyle(color: _indicatorColorList[0]))),
            // Center(key:ValueKey('2'),child: Text('音乐',style:TextStyle(color: _indicatorColorList[1]))),
            // Center(key:ValueKey('3'),child: Text('篮球',style:TextStyle(color: _indicatorColorList[2]))),
            ...<String>['电影', '音乐', '篮球'].asMap().entries.map((entry) {
              final int index = entry.key;
              final String value = entry.value;
              return Center(
                child: Container(
                  width:200,
                  height:200,
                  color:_colorAnimation.value,
                  child: Text(value,
                      style: TextStyle(color: _indicatorColorList[index])),
                ),
              );
            }).toList()
          ],
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: _incrementCounter,
          tooltip: 'Increment',
          child: const Icon(Icons.add),
        ), // This trailing comma makes auto-formatting nicer for build methods.
      ),
    );
  }
}
